import CryptoJS from 'crypto-js';

//加密
function encrypt(word: string, keyStr?: string) {
  keyStr = keyStr ? keyStr : '6y3hgfcmv4t6b9ft'; //判断是否存在ksy，不存在就用定义好的key
  const key = CryptoJS.enc.Utf8.parse(keyStr);
  const srcs = CryptoJS.enc.Utf8.parse(word);
  const encrypted = CryptoJS.AES.encrypt(srcs, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });
  return encrypted.toString();
}
//解密
function decrypt(word: string, keyStr?: string) {
  keyStr = keyStr ? keyStr : '6y3hgfcmv4t6b9ft';
  const key = CryptoJS.enc.Utf8.parse(keyStr);
  const decrypt = CryptoJS.AES.decrypt(word, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });
  return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}

export default {
  encrypt,
  decrypt
};
